package com.wzd.taijiapp.base

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.sda.huoshui.api.RetrofitHelper
import com.sda.huoshui.api.RetrofitService
import com.sda.huoshui.dialog.WaitDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import rx.subscriptions.CompositeSubscription

abstract class BaseFragment : Fragment() {

    var retrofitService: RetrofitService? = null
    var mCompositeSubscription: CompositeSubscription? = null
    private var lastClickTime: Long = 0
    private var waitDialog: WaitDialog? = null
    var pageNo = 1
    protected var isFront = false

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        isFront = isVisibleToUser
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        isFront = hidden
    }

    override fun onResume() {
        super.onResume()
        isFront = true
    }

    override fun onPause() {
        super.onPause()
        isFront = false
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(initView(), container, false)
    }

    @Subscribe
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        retrofitService = RetrofitHelper.getInstance(requireContext()).server
        mCompositeSubscription = CompositeSubscription()
        initData()
    }

    abstract fun initView(): Int

    abstract fun initData()

    /**
     * 跳转页面
     */
    fun goToActivity(intent: Intent) {
        startActivity(intent)
    }

    /**
     * 是否快速点击了
     */
    protected fun fastClick(): Boolean {
        if (System.currentTimeMillis() - lastClickTime <= 500) {
            return true
        }
        lastClickTime = System.currentTimeMillis()
        return false
    }



    fun showProgressDialog() {
        if (waitDialog == null) {
            waitDialog = WaitDialog(requireContext())
            waitDialog!!.setCancelable(false)
        }
        if (!waitDialog!!.isShowing && isFront) {
            waitDialog!!.show()
        }
    }

    /**
     * 取消加载dialog
     */
    fun cancelProgressDialog() {
        if (waitDialog != null && waitDialog!!.isShowing) {
            waitDialog!!.dismiss()
        }
    }

    override fun onDestroy() {
        //取消订阅防止内存泄露
        super.onDestroy()

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }

        if (mCompositeSubscription!= null && mCompositeSubscription!!.hasSubscriptions()) {
            mCompositeSubscription!!.unsubscribe()
        }
    }
}